<?php

namespace app\adminapiv3\service;


class HostAccessLog extends Base
{
    /**
     * @name 列表
     * @param $hostId
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/31 15:41
     */
    function list($hostId, $params, $limit = 10)
    {
        $server = new \app\common\service\HostAccessLog();
        $field = 'id,host_id, source_ip, target_ip';
        $list = $server->list($hostId, $params, $limit, $field, 'list');

        //数据处理
        foreach($list['data'] as $k => $v){
            $totalWhere['source_ip'] = $v['source_ip'];
            $totalWhere['target_ip'] = $v['target_ip'];
            $totalWhere['host_id'] = $v['host_id'];
            $v['id'] = (string) $v['id'];
            $v['total'] = $server->gettotal($totalWhere);//总条数
            $v['total_out'] = $server->getTotalOut($totalWhere);//总出栈条数
            $v['total_input'] = $server->getTotalInput($totalWhere);//总入栈条数

            $list['data'][$k] = $v;
        }

        return $list;
    }

    /**
     * @name 详情
     * @param $hostId
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/31 16:57
     */
    function detail($hostId, $params, $limit = 10)
    {
        $server = new \app\common\service\HostAccessLog();
        $field = 'id, source_ip, direction, target_ip, target_port, protocols, permission, first_time, last_time, view_all';
        $list = $server->list($hostId, $params, $limit, $field);

        //数据处理
        foreach($list['data'] as $k => $v){
            $v['id'] = (string) $v['id'];
            $v['first_time'] = date('Y-m-d H:i:s', $v['first_time']);
            $v['last_time'] = date('Y-m-d H:i:s', $v['last_time']);

            $list['data'][$k] = $v;
        }

        return $list;
    }

    /**
     * @name 日志开关
     * @author wx
     * @date time
     */
    function logSwitch($hostId, $status)
    {

        $HostLearnConfig = new \app\common\service\HostLearnConfig();

        //2. 修改日志开关
        $HostLearnConfig->modifyLogStatus($hostId, $status);

        return true;
    }

    /**
     * @name 读取日志选项
     * @param $hostId
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/11/1 10:43
     */
    function readConfig($hostId)
    {
        $logConfigDb = new \app\common\service\HostAccessLogConfig();
        $configInfo = $logConfigDb->findData(['host_id' => $hostId]);

        return $configInfo;
    }

    /**
     * @name 修改日志选项
     * @param $hostId
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/11/1 10:47
     */
    function modifyConfig($hostId, $updateData)
    {
        $logConfigDb = new \app\common\service\HostAccessLogConfig();
        $condition[] = array('host_id', 'in', $hostId);
        $logConfigDb->modify($condition, $updateData);

        return true;
    }


    /**
     * @name 修改忽略
     * @param $hostId
     * @param $id
     * @param $updateData
     * @author wx
     * @date 2019/11/1 13:21
     */
    function modifyIgnore($hostId, $id, $updateData)
    {

        //2. 修改日志选项
        $server = new \app\common\service\HostAccessLog();
        $condition[] = array('id', 'in', $id);
        $server->modify($condition, $updateData);

        return true;
    }

    /**
     * @name: 告警列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-09 19:11:54
     */
    function warringList ($filterData, $limit = 10)
    {
        $HostAccessLog = new \app\common\service\HostAccessLog();

        $return = $HostAccessLog->toDo('list', [$filterData['host_id'], $filterData, $limit, '*']);

        return $return;
    }
}